/*
 * @Author: doing
 * @Date: 2023-08-10 10:20:38
 * @LastEditors: git config user.name && git config user.email
 * @LastEditTime: 2023-09-02 17:52:27
 * @FilePath: \background-management-system\src\router\index.js
 * @Description: 
 * 动态路由js文件
 * Copyright (c) 2023 by 1540265624@qq.com, All Rights Reserved. 
 */

import {
    createRouter,
    createWebHashHistory
} from 'vue-router'

//基本路由组件
import Admin from '~/layouts/admin.vue'
import NotFound from '~/pages/404.vue'

//动态路由组件
import Index from '~/pages/index.vue'
import UserManager from '~/pages/user/index.vue'
import RoleManager from '~/pages/role/index.vue'
import MenuManager from '~/pages/menu/index.vue'
import PicManager from '~/pages/picture/index.vue'
import VideoManager from '~/pages/video/index.vue'
import MemberManager from '~/pages/member/index.vue'
import AdvertManager from '~/pages/advert/index.vue'
import OfficialManager from '~/pages/official/index.vue'


//默认路由,由所有用户共享
const routes = [
    {
        path: '/',
        name: 'admin',
        component: Admin,
        redirect: '/dashboard'
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: NotFound
    }
]

//异步添加路由
const asyncRoutes = [
    {
        path:"/dashboard",
        name:"后台首页",
        component:Index,
        meta:{
            title:"后台首页"
        }
    },
    {
        path:"/system/user",
        name:"用户管理",
        component:UserManager,
        meta:{
            title:"用户管理"
        }
    },
    {
        path:"/system/role",
        name:"角色管理",
        component:RoleManager,
        meta:{
            title:"角色管理"
        }
    },
    {
        path:"/system/menu",
        name:"菜单管理",
        component:MenuManager,
        meta:{
            title:"菜单管理"
        }
    },
    {
        path:"/content/photo",
        name:"图片管理",
        component:PicManager,
        meta:{
            title:"图片管理"
        }
    },
    {
        path:"/content/video",
        name:"视频管理",
        component:VideoManager,
        meta:{
            title:"视频管理"
        }
    },
    {
        path:"/content/member",
        name:"成员管理",
        component:MemberManager,
        meta:{
            title:"成员管理"
        }
    },
    {
        path:"/advert/index",
        name:"广告管理",
        component:AdvertManager,
        meta:{
            title:"广告管理"
        }
    },
    {
        path:"#",
        name:"线狮子少年官网",
        component:OfficialManager,
        meta:{
            title:"线狮子少年官网"
        }
    }
]


//动态将组件添加到路由中去(注意菜单显示是在FMenu组件中调用了$store.state.menu然后才显示出菜单左侧)
export const router = createRouter({
    history: createWebHashHistory(),
    routes
})

//动态添加路由方法
/**
 * 将本来全部可以动态添加的路由和返回回来的路由数据对比,如果返回回来的数据有在本来的动态路由中,则添加到routes中去.
 * @param {Array} menus - 菜单数组
 * @returns {boolean}  是否有新的路由
 */
export function addRoutes(menus){
    // 是否有新的路由
    let hasNewRoutes = false
    const findAndAddRoutesByMenus = (arr) =>{
        arr.forEach(e=>{
            let item = asyncRoutes.find(o=>o.path == e.url)
            if(item && !router.hasRoute(item.path)){
                router.addRoute("admin",item)
                // console.log(item)
                hasNewRoutes = true
            }
            if(e.children && e.children.length > 0){
                findAndAddRoutesByMenus(e.children)
            }
        })
    }

    findAndAddRoutesByMenus(menus)

    return hasNewRoutes
}
